// @algorithm @lc id=438 lang=cpp 
// @title find-all-anagrams-in-a-string


// @test("cbaebabacd","abc")=[0,6]
// @test("abab","ab")=[0,1,2]
class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        int n = s.length(), m = p.length();
        vector<int> as;
        if(n<m) return as;
        vector<int> sc(26,0), pc(26,0);
        for(int i=0; i<m; i++){
            ++ sc[ s[i]-'a' ];
            ++ pc[ p[i]-'a' ];
        }
        if(sc == pc) as.push_back(0);
        for(int j=m; j<n; j++){
            --sc[ s[j-m]-'a' ];
            ++sc[ s[j]-'a' ];
            if(sc == pc) as.push_back(j-m+1);
        }
        return as;
    }
};